//
//  MainViewController.m
//  QinrenManagers
//
/*
             _ooOoo_
            o8888888o
            88" . "88
            (| -_- |)
            O\  =  /O
         ____/`---'\____
        .'  \\|     |//  `.
        /  \\|||  :  |||//  \
       /  _||||| -:- |||||-  \
       |   | \\\  -  /// |   |
       | \_|  ''\---/''  |   |
       \  .-\__  `-`  ___/-. /
    ___`. .'   /--.--\  `. . __
    ."" '<  `.___\_<|>_/___.'  >'"".
   | | :  `- \`.;`\ _ /`;.`/ - ` : | |
   \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/
//  Created by lemon on 15/5/22.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "MainViewController.h"

//视图
#import "HomeView.h"

//模型
#import "ScrollADModel.h"

#import "PolicyModel.h"

//聊天
#import "HealthManagerViewController.h"
//#import "ContactsViewController.h"
#import "ChatViewController.h"


#import "ChatListViewController.h"
#import "ApplyViewController.h"

//控制器
#import "GroupListViewController.h"
//#import "ManagerGroupViewController.h"
#import "WorkGroupViewController.h"
#import "CustomerServiceViewController.h"
#import "BookingRegistViewController.h"
#import "GroupPerformanceViewController.h"
#import "PersonalPerformanceViewController.h"
#import "BMAdScrollView.h"

//广告
#import "ADDetailsViewController.h"

#import "SalesPolicyViewController.h"
#import "PolicyModel.h"
#import "DetailSaleViewController.h"

#import "UIImageView+EMWebCache.h"

@interface MainViewController ()<IChatManagerDelegate,ValueClickDelegate>

@property (nonatomic, strong) NSMutableArray * adDataSource;
@property (nonatomic, strong) NSMutableArray * policyModelArry;//底部政策模型

@end

@implementation MainViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _adDataSource = [NSMutableArray array];
        _policyModelArry = [NSMutableArray array];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self requestForADData];
}

- (void)requestForADData {
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSDictionary *parameters = @{@"type":@"4",@"imgwidth":@"540",@"imgheight":@"280"};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"get.ad.list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"广告的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
                for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                    ScrollADModel *scrollADModel = [[ScrollADModel alloc]init];
                    [scrollADModel setValuesForKeysWithDictionary:dataDic];
                    [_adDataSource addObject:scrollADModel];
                
                }
            
            [self creatUI];
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
    
}




- (void)creatUI {
    HomeView *homeView = [[HomeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    _policyModelArry = homeView.policyModelArry;
    homeView.sendBlock = ^(UIButton *sender){
        [self btnClick:sender];
    };
    
    //NSArray * tempImageArr = @[@"one",@"two",@"three",@"four"];
    //滚动条
    NSMutableArray *viewsArray = [@[] mutableCopy];
    NSMutableArray *adImagesArray = [NSMutableArray array];
    NSMutableArray *adTitleArray = [NSMutableArray array];
    for (int i = 0; i < _adDataSource.count; ++i) {
        
        ScrollADModel * sdModel = [_adDataSource objectAtIndex:i];
        [adImagesArray addObject:sdModel.adpic];
        [adTitleArray addObject:@""];
        UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, (SCREEN_H-49-64)/3)];
        tempImageView.contentMode = UIViewContentModeScaleToFill;
        tempImageView.userInteractionEnabled = YES;
        //[tempImageView setImage:[UIImage imageNamed:[tempImageArr objectAtIndex:i]]];
        [tempImageView sd_setImageWithURL:[NSURL URLWithString:sdModel.adpic] placeholderImage:[UIImage imageNamed:@"touxiang.jpg"]];
        
        [viewsArray addObject:tempImageView];
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    BMAdScrollView *adScrollView = [[BMAdScrollView alloc]initWithNameArr:adImagesArray titleArr:adTitleArray height:(SCREEN_H-49-64)/3 offsetY:0];
    
    adScrollView.vDelegate = self;
    adScrollView.pageCenter = CGPointMake(SCREEN_W/2, (SCREEN_H-49-64)/3-15);
    
    [self.view addSubview:homeView];
    [self.view addSubview:adScrollView];
}


#pragma mark ---------广告点击代理-----------
- (void)buttonClick:(int)vid {

    ADDetailsViewController *adDetailsViewController = [[ADDetailsViewController alloc]init];

    ScrollADModel * sdModel = [_adDataSource objectAtIndex:(vid-1)];

    adDetailsViewController.adID = sdModel.ad_id;

    [self.navigationController pushViewController:adDetailsViewController animated:YES];

}

#pragma mark -------------------------点击事件------------------------------

- (void)btnClick:(UIButton *)sender {

    switch (sender.tag) {
        case 1000:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            HealthManagerViewController *healthManagerViewController = [[HealthManagerViewController alloc]init];
            healthManagerViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:healthManagerViewController animated:NO];
            
        }
            break;
        case 1001:
        {
            NSLog(@"%ld ",(long)sender.tag);
// 
            WorkGroupViewController * chatOnlineViewController = [[WorkGroupViewController alloc] init];
            
            chatOnlineViewController.title = @"工作群";
            chatOnlineViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:chatOnlineViewController animated:NO];
        }
            break;
        case 1002:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            CustomerServiceViewController *customerServiceViewController = [[CustomerServiceViewController alloc]init];
            
            customerServiceViewController.title = @"客户方案";
            customerServiceViewController.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:customerServiceViewController animated:NO];
            
        }
            break;
        case 1003:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            BookingRegistViewController *bookingRegistViewController = [[BookingRegistViewController alloc]init];
            
            bookingRegistViewController.title = @"服务预约";
            bookingRegistViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:bookingRegistViewController animated:NO];

        }
            break;
        case 1004:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            PersonalPerformanceViewController *personalPerformanceViewController = [[PersonalPerformanceViewController alloc]init];
            
            personalPerformanceViewController.title = @"我的个人业绩";
            personalPerformanceViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:personalPerformanceViewController animated:NO];
            
        }
            break;
        case 1005:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            GroupPerformanceViewController *groupPerformanceViewController = [[GroupPerformanceViewController alloc]init];
            
            groupPerformanceViewController.title = @"我的团队业绩";
            groupPerformanceViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:groupPerformanceViewController animated:NO];
            
        }
            break;
          
        case 1006:
        {
            NSLog(@"%ld ",(long)sender.tag);
            
            SalesPolicyViewController * salesPolicyViewController = [[SalesPolicyViewController alloc]init];
            
            salesPolicyViewController.title = @"营销政策";
            salesPolicyViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:salesPolicyViewController animated:NO];
            
        }
            break;
            
        case 2000:
        {
            DetailSaleViewController *detailSaleViewController = [[DetailSaleViewController alloc] init];
            PolicyModel *policyModel = _policyModelArry[0];
            detailSaleViewController.title = @"政策详情";
            detailSaleViewController.policyTitle = [policyModel.name stringByReplacingOccurrencesOfString:@"+" withString:@""];
            detailSaleViewController.article_id = policyModel.aid;
            [self.navigationController pushViewController:detailSaleViewController animated:NO];
            
        }
            break;
            
        case 2001:
        {
            DetailSaleViewController *detailSaleViewController = [[DetailSaleViewController alloc] init];
            PolicyModel *policyModel = _policyModelArry[1];
            detailSaleViewController.title = @"政策详情";
            detailSaleViewController.policyTitle = [policyModel.name stringByReplacingOccurrencesOfString:@"+" withString:@""];
            detailSaleViewController.article_id = policyModel.aid;
            [self.navigationController pushViewController:detailSaleViewController animated:NO];
            
        }
            break;
            
        case 2002:
        {
            DetailSaleViewController *detailSaleViewController = [[DetailSaleViewController alloc] init];
            PolicyModel *policyModel = _policyModelArry[2];
            detailSaleViewController.title = @"政策详情";
            detailSaleViewController.policyTitle = [policyModel.name stringByReplacingOccurrencesOfString:@"+" withString:@""];
            detailSaleViewController.article_id = policyModel.aid;
            [self.navigationController pushViewController:detailSaleViewController animated:NO];
        }
            break;
            
        default:
            break;
    }

}





@end




